package com.netbao.sys.controller;

import com.netbao.model.PageBean;
import com.netbao.model.PageResult;
import com.netbao.model.Result;
import com.netbao.sys.model.User;
import com.netbao.sys.service.RoleService;
import com.netbao.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sys/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @RequiresPermissions(value = "sys:user:save")
    @PostMapping("/save")
    @ResponseBody
    public Result add(User user){
        return userService.addUser(user);
    }

    @GetMapping("/getbyid/{id}")
    @ResponseBody
    public Result add(@PathVariable Integer id){
        return userService.getById(id);
    }

    @PutMapping("/update")
    @ResponseBody
    public Result upDate(@RequestBody User user){
        return userService.upDate(user);
    }

    @RequiresPermissions("role:manage:delete")
    @PostMapping("/delete")
    @ResponseBody
    public Result del(Integer id){
        return userService.del(id);
    }

    @GetMapping("/page")
    public String toPage(){
        return "/sys/user";
    }

    @GetMapping("/list")
    @ResponseBody
    public PageResult list(PageBean pageBean, String userName){
        return userService.list(pageBean,userName);
    }

    @PostMapping("saverole")
    @ResponseBody
    public Result saveRole(String roleIds,Long userId,String cnames){
        return userService.saveRole(roleIds,userId,cnames);
    }

    @GetMapping("/getrole")
    @ResponseBody
    public PageResult getRole(){
        return userService.getRole();
    }

    @GetMapping("/getfirst")
    @ResponseBody
    public Result getFirst(){
        return userService.getFirst();
    }

//    @ResponseBody
//    @RequestMapping("/login")
//    public Map<String,Object> login(String imageCode, @Valid User user, BindingResult bindingResult, HttpSession session){
//        Map<String,Object> map=new HashMap<String,Object>();
//        if(StringUtil.isEmpty(imageCode)){
//            map.put("success", false);
//            map.put("errorInfo", "请输入验证码!");
//            return map;
//        }
//        if(bindingResult.hasErrors()){
//            map.put("success", false);
//            map.put("errorInfo", bindingResult.getFieldError().getDefaultMessage());
//            return map;
//        }
//        Subject subject= SecurityUtils.getSubject();
//        UsernamePasswordToken token=new UsernamePasswordToken(user.getAccount(), user.getPassword());
//        try{
//            subject.login(token);
//            User account=(User) SecurityUtils.getSubject().getPrincipal();
//            User currentUser=userService.findByAccount(account.getAccount());
//            session.setAttribute("currentUser", currentUser);
//            List<Role> roleList=roleService.findByUserId((long) currentUser.getId().intValue());
//            map.put("roleList", roleList);
//            map.put("roleSize", roleList.size());
//            map.put("success", true);
//            //logService.save(new Log(Log.LOGIN_ACTION,"用户登录"));
//            return map;
//        }catch(Exception e){
//            e.printStackTrace();
//            map.put("success", false);
//            map.put("errorInfo", "用户名或者密码错误!");
//            return map;
//        }
//    }
}
